import KoaRouter from 'koa-router'
import { Models } from '@/common/typings/modle'

import { command } from '@/server/mysql'
import { Success } from '@/core/HttpException'
import { format } from '@/common/utils/date'
import Config from '@/config/Config'

import validator from '@/middlewares/validator'
import schema from '@/common/api-json-schema/system/auth/register'

const router = new KoaRouter({
  prefix: `${Config.API_PREFIX}v1/system/auth`,
})

router.post('/register', validator(schema, 'body'), async (ctx: Models.Ctx) => {
  const { password, username, email } = ctx.request.body as any
  const date = format(new Date())
  await command(`
  INSERT INTO system_user ( user_name, email, password, created_at, updated_at)
  VALUES
  ( '${username}', '${email}', '${password}', '${date}', '${date}' );`)
  // ctx.response.body = {
  //   ...new Success()
  // }
  throw new Success()
})
export default router
